var errorTiming
function errorShow ($this,msg) {
  if($this.hasClass("error-item")){
    clearTimeout(errorTiming)
    $("#errorInfo").html(msg)
    $(".error-box").removeClass("hide")
    errorTiming = setTimeout(function(){
      $(".error-box").addClass("hide")
    },2000)
  }
}
function isNull($this) {
  if ($this.val() === '') {
    $this.addClass("error-item")
    errorShow($this,$this.attr("placeholder")+'不能为空！')
    return true
  } else {
    $this.removeClass("error-item")
    return false
  }
}
function isPhone($this) {
  var value = $this.val()
  !(/^1[34578]\d{9}$/.test(value))
    ? $this.addClass("error-item")
    : $this.removeClass("error-item")
  errorShow($this,'请输入正确的手机号')
}
function isEmail($this) {
  var value = $this.val()
  var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
  !reg.test(value)
    ? $this.addClass("error-item")
    : $this.removeClass("error-item")
    errorShow($this,'请输入正确的邮箱')
}
var postCodeCheck
function isCode () {
  var $this = $("#form_code")
  var value = $this.val()
  console.log(1111)
  if(value == ''){
    $this[0].focus()
    errorShow($this,'验证码不能为空')
    return
  }
  $this.removeClass("error-item")
  if(!postCodeCheck){
    $this.addClass("error-item")
  }
  value != postCodeCheck
    ? $this.addClass("error-item")
    : $this.removeClass("error-item")
    errorShow($this,'验证码不正确或已过期')
}
function getPostCode(){
  $.ajax({
    type: 'GET',
    url: ajaxUrlObj['GET_POST_CODE'],
    success: function(rep){
      if (typeof rep === 'string') {
        rep = JSON.parse(rep)
      }
      if (rep.code == 1){
        rep = JSON.parse(rep.data)
        $("#postCodeImg").attr("src",rep.url).removeClass("hide");
        $(".reflesh").removeClass("hide");
        postCodeCheck = rep.value
      }
    },
    error: function(error){
      console.log(error)
    }
  })
}

$(window).ready(function(){
  $(".msg-box .is-required").on("blur", function () {
    var $this = $(this)
    isNull($this)
  })
  $("#form_phone").on("change", function () {
    var $this = $(this)
    isPhone($this)
  })
  $("#form_email").on("change", function () {
    var $this = $(this)
    isEmail($this)
  })
  $("#form_code").one("focus",function(){
    getPostCode()
  })
  $("#postCodeImg,.reflesh").on("click",function(){
    getPostCode()
  })
  $("#submitForm").on("click", function () {
    isNull($("#form_text"))
    isNull($("#form_code"))
    isCode()
    var errorNum = $(".error-item").length
    if (errorNum > 0) {
      return
    }
    $.ajax({
      type: 'POST',
      url: ajaxUrlObj['POST_MESSAGE'],
      data: {
        content:$("#form_text").val(),
        customerName:$("#form_name").val(),
        tel:$("#form_phone").val(),
        email:$("#form_email").val()
      },
      success: function(rep){
        if (typeof rep === 'string') {
          rep = JSON.parse(rep)
        }
        $(".toast-box .toast-body").html(rep.code == 1?'反馈成功':'反馈失败')
        $(".toast-box").removeClass("hide");
        setTimeout(function(){
          $(".toast-box").addClass("hide").removeClass("error-toast");
          window.location.reload()
        }, 1500);
      },
      error: function(error){
        console.log(error)
      }
    });
  })
})